package com.feng.chat.service.impl;

import com.feng.chat.common.Common;
import com.feng.chat.dto.KeepAlive;
import com.feng.chat.dto.Result;
import com.feng.chat.entity.TalkRecords;
import com.feng.chat.entity.TalkRecordsCode;
import com.feng.chat.entity.Users;
import com.feng.chat.mapper.GroupMemberMapper;
import com.feng.chat.mapper.TalkRecordsCodeMapper;
import com.feng.chat.mapper.TalkRecordsMapper;
import com.feng.chat.mapper.UsersMapper;
import com.feng.chat.service.ITalkRecordsCodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.chat.utils.GetUserUtil;
import com.feng.chat.vo.CodeBlockVO;
import com.feng.chat.vo.EventTalkDataVO;
import com.feng.chat.vo.EventTalkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 用户聊天记录_代码块消息表 服务实现类
 * </p>
 *
 * @author dyf
 * @since 2022-05-31
 */
@Service
public class TalkRecordsCodeServiceImpl extends ServiceImpl<TalkRecordsCodeMapper, TalkRecordsCode> implements ITalkRecordsCodeService {

    @Autowired
    private TalkRecordsMapper talkRecordsMapper;

    @Autowired
    private TalkRecordsCodeMapper talkRecordsCodeMapper;

    @Autowired
    private GroupMemberMapper groupMemberMapper;

    @Autowired
    private UsersMapper usersMapper;

    /**
     * 发送代码消息
     * @param uid
     * @param code
     * @param lang
     * @param receiver_id
     * @param talk_type
     * @return
     */
    @Override
    public Result messageCode(Integer uid, String code, String lang, Integer receiver_id, Integer talk_type) {
        TalkRecords talkRecords = new TalkRecords();
        talkRecords.setTalkType(talk_type);
        talkRecords.setMsgType(4);
        if (talk_type==2){
            talkRecords.setGroupRead(uid.toString());
        }
        talkRecords.setUserId(uid);
        talkRecords.setReceiverId(receiver_id);
        talkRecordsMapper.insert(talkRecords);

        TalkRecordsCode talkRecordsCode = new TalkRecordsCode();
        talkRecordsCode.setRecordId(talkRecords.getId());
        talkRecordsCode.setUserId(uid);
        talkRecordsCode.setLang(lang);
        talkRecordsCode.setCode(code);
        talkRecordsCodeMapper.insert(talkRecordsCode);

        KeepAlive keepAlive = new KeepAlive();
        keepAlive.setEvent(Common.EVENT_TALK);

        EventTalkVO eventTalkVO = new EventTalkVO();
        eventTalkVO.setTalk_type(talkRecords.getTalkType());
        eventTalkVO.setReceiver_id(talkRecords.getReceiverId());
        eventTalkVO.setSender_id(talkRecords.getUserId());

        EventTalkDataVO eventTalkDataVO = new EventTalkDataVO();
        eventTalkDataVO.setUser_id(talkRecordsCode.getUserId());
        eventTalkDataVO.setTalk_type(talkRecords.getTalkType());
        eventTalkDataVO.setReceiver_id(talkRecords.getReceiverId());
        eventTalkDataVO.setMsg_type(talkRecords.getMsgType());
        eventTalkDataVO.setIs_revoke(talkRecords.getIsRevoke());
        eventTalkDataVO.setIs_read(talkRecords.getIsRead());
        eventTalkDataVO.setIs_mark(talkRecords.getIsMark());
        eventTalkDataVO.setId(talkRecords.getId());
        eventTalkDataVO.setCreated_at(talkRecords.getUpdatedAt().format(DateTimeFormatter.ofPattern(Common.LOCALDATETIME_FORMATER)));
        Users users = usersMapper.selectById(talkRecords.getUserId());
        eventTalkDataVO.setNickname(users.getNickName());
        eventTalkDataVO.setAvatar(users.getAvatar());

        CodeBlockVO codeBlockVO = new CodeBlockVO();
        codeBlockVO.setUser_id(talkRecordsCode.getUserId());
        codeBlockVO.setLang(talkRecordsCode.getLang());
        codeBlockVO.setId(talkRecordsCode.getId());
        codeBlockVO.setCreated_at(talkRecordsCode.getCreatedAt().format(DateTimeFormatter.ofPattern(Common.LOCALDATETIME_FORMATER)));
        codeBlockVO.setRecord_id(talkRecordsCode.getRecordId());
        codeBlockVO.setCode(talkRecordsCode.getCode());
        eventTalkDataVO.setCode_block(codeBlockVO);

        eventTalkVO.setData(eventTalkDataVO);
        keepAlive.setContent(eventTalkVO);

        GetUserUtil.sendMessage(keepAlive, uid);
        if (talk_type == 1) {
            if (GetUserUtil.isOnLine(receiver_id) == 1) {
                GetUserUtil.sendMessage(keepAlive, receiver_id);
            }
        } else {
            List<Integer> groupMember = groupMemberMapper.getGroupMember(receiver_id);
            groupMember.remove(uid);
            for (Integer user_id : groupMember) {
                if (GetUserUtil.isOnLine(user_id) == 1) {
                    GetUserUtil.sendMessage(keepAlive, user_id);
                }
            }
        }

        return Result.success();
    }
}
